package com.dhecp.project.quartz.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.dhecp.project.api.domain.ApiResponse;
import com.dhecp.project.api.domain.PurchaseOrder;
import com.dhecp.project.api.mapper.PurchaseOrderMapper;
import com.dhecp.project.api.service.Api1688Service;
import com.dhecp.project.api.service.ApiLogService;

/**
 * @ClassName Ali1688Task
 * @Description 1688接口定时任务
 * @Author alula
 * @Date 2022-08-31 19:10
 */
@Component("ali1688Task")
public class Ali1688Task {

    private static final Logger logger = LoggerFactory.getLogger(Ali1688Task.class);
    //1688系统名称
    private static final String OPEN1688_SYSTEM_NAME = "1688";

    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;

    @Autowired
    private Api1688Service api1688Service;

    @Autowired
    private ApiLogService apiLogService;

    /**
     * 定时创建1688采购订单(每天7：00运行)
     */
    public void create1688Order()
    {
        try {
            String type = "Create1688Order";
            Map<String, Object> queryParams = new HashMap<String, Object>();
            queryParams.put("filterCreate", 1);
            List<PurchaseOrder>  dataList = purchaseOrderMapper.findPurchaseOrderList(queryParams);
            for (PurchaseOrder itemData: dataList) {
                Map<String, Object> dataParams = new HashMap<>();
                dataParams.put("orderNos", itemData.getBid());
                dataParams.put("userId", "0000");
               ApiResponse apiResponse = api1688Service.create1688Order(OPEN1688_SYSTEM_NAME, type, dataParams);
                if (apiResponse.getCode() == 500){
                	apiLogService.addApiLog(OPEN1688_SYSTEM_NAME, type, "定时创建1688采购订单，订单号："+itemData.getBid() +"，dataParams：" + dataParams, "ECP_API调用创建1688订单接口出错，错误为："+apiResponse.getMessage(), apiResponse.getCode(), apiResponse.getMessage());
                }
            }
        }catch (Exception ex){
            logger.error("【1688接口】定时创建1688订单出错，错误信息为：" + ex.getMessage());
        }
    }

    /**
     * 定时获取1688订单详情信息(每3个小时运行一次)
     */
    public void get1688OrderDetail(){
        try {
            String type = "Get1688OrderDetail";
            Map<String, Object> queryParams = new HashMap<String, Object>();
            queryParams.put("filterSuccess", 1);
            List<PurchaseOrder>  dataList = purchaseOrderMapper.findPurchaseOrderList(queryParams);
            Map<String, Object> dataParams = null;
            for (PurchaseOrder itemData: dataList) {
                dataParams = new HashMap<>();
                dataParams.put("orderNos", itemData.getBid());
                ApiResponse apiResponse = api1688Service.get1688OrderDetail(OPEN1688_SYSTEM_NAME, type, dataParams);
                if (apiResponse.getCode() == 500){
                	apiLogService.addApiLog(OPEN1688_SYSTEM_NAME, type, "dataParams：" + JSON.toJSONString(dataParams), "ECP_API调用获取1688订单详情接口出错，错误为："+apiResponse.getMessage(), apiResponse.getCode(), apiResponse.getMessage());
                }
            }
        }catch (Exception ex){
            logger.error("【1688接口】定时获取1688订单详情出错，错误信息为：" + ex.getMessage());
        }
    }

    /**
     * 定时获取1688订单物流信息(每2个小时运行一次)
     */
    public void get1688OrderLogisticsInfo(){
        try {
            String type = "Get1688OrderLogisticsInfo";
            Map<String, Object> queryParams = new HashMap<String, Object>();
            //queryParams.put("filterSuccess", 1);
            queryParams.put("filterWlEmpty", 1);//只以创建成功的订单与运单号为空的
            List<PurchaseOrder>  dataList = purchaseOrderMapper.findPurchaseOrderList(queryParams);
            Map<String, Object> dataParams = null;
            for (PurchaseOrder itemData: dataList) {
                dataParams = new HashMap<>();
                dataParams.put("orderNo", itemData.getBid());
                ApiResponse apiResponse = api1688Service.get1688OrderLogisticsInfo(OPEN1688_SYSTEM_NAME, type, dataParams);
                if (apiResponse.getCode() == 500){
                	apiLogService.addApiLog(OPEN1688_SYSTEM_NAME, type, "dataParams：" + JSON.toJSONString(dataParams), "ECP_API调用获取1688订单物流信息接口出错，错误为："+apiResponse.getMessage(), apiResponse.getCode(), apiResponse.getMessage());
                }
            }
        }catch (Exception ex){
            logger.error("【1688接口】定时获取1688订单物流信息出错，错误信息为：" + ex.getMessage());
        }
    }
}
